//
//  PhoneRechargeBlueButton.m
//  Terran
//
//  Created by 李勋 on 16/3/6.
//  Copyright © 2016年 yuwubao. All rights reserved.
//

#import "PhoneRechargeBlueButton.h"

@implementation PhoneRechargeBlueButton

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
//- (id)initWithCoder:(NSCoder *)aDecoder
//{
//    if (self == [super initWithCoder:aDecoder]) {
//        [self setup];
//    }
//    return self;
//}
- (void)awakeFromNib
{
    self.layer.borderWidth   = .5f;
    self.layer.cornerRadius  = 5;
    self.layer.borderColor   = [UIColor lightGrayColor].CGColor;
    self.layer.masksToBounds = YES;
    self.titleLabel.font     = [UIFont fontWithName:FONT_NAME size:17.0];
    self.titleLabel.textAlignment = NSTextAlignmentCenter;
    self.titleLabel.lineBreakMode = NSLineBreakByWordWrapping;
}

- (NSMutableAttributedString *)formAttributedTitle:(NSString *)titleString andEnable:(BOOL)enable
{
    NSMutableAttributedString *title = [[NSMutableAttributedString alloc] initWithString:titleString];
    
    [title addAttribute:NSForegroundColorAttributeName value:enable ? UIColorFromRGBA(kCommonBlueColor, 1.f) :[UIColor lightGrayColor] range:NSMakeRange(0, title.length)];
    
    NSRange range = [titleString rangeOfString:@"\n"];
    //
    [title addAttribute:NSFontAttributeName value:[UIFont fontWithName:FONT_NAME size:12.0] range:NSMakeRange( range.location+range.length, title.length - (range.location+range.length))];
    NSMutableParagraphStyle * paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    [paragraphStyle setLineSpacing:4];
    paragraphStyle.alignment = NSTextAlignmentCenter;
    [title addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [title length])];
    
    return title;
}
- (void)setIsAllowChoosing:(BOOL)isAllowChoosing
{
    _isAllowChoosing = isAllowChoosing;
    
    self.enabled = _isAllowChoosing;
    if (_isAllowChoosing) {
        self.layer.borderColor = UIColorFromRGBA(kCommonBlueColor, 1.f).CGColor;
        
//        NSMutableAttributedString *str = [self.titleLabel.attributedText mutableCopy];
//        [str addAttribute:NSForegroundColorAttributeName value:UIColorFromRGBA(kCommonBlueColor, 1.f)  range:NSMakeRange(0, str.length)];
//        [self setAttributedTitle:str forState:UIControlStateNormal];
        
        
    }else{
        
        self.layer.borderColor = [UIColor lightGrayColor].CGColor;
//        NSMutableAttributedString *str = [self.titleLabel.attributedText mutableCopy];
//        [str addAttribute:NSForegroundColorAttributeName value:[UIColor lightGrayColor]  range:NSMakeRange(0, str.length)];
//        [self setAttributedTitle:str forState:UIControlStateNormal];
    }
}

- (void)setTitle:(NSString *)title forState:(UIControlState)state
{
    NSLog(@"title %@",title);
    [super setTitle:title forState:state];
    
    
    [self setAttributedTitle:[self formAttributedTitle:title andEnable:self.isAllowChoosing] forState:state];
//    NSString *string = title;
//        //[string
//    NSMutableAttributedString *str = [self.titleLabel.attributedText mutableCopy];
//       // [NSFont bo]
//    NSRange range = [string rangeOfString:@"\n"];
//    
//        [str addAttribute:NSFontAttributeName value:[UIFont fontWithName:FONT_NAME size:11.0] range:NSMakeRange( range.location+range.length, str.length - (range.location+range.length))];
//        [self setAttributedTitle:str forState:UIControlStateNormal];
//    [self layoutIfNeeded];
}
- (void) setHighlighted:(BOOL)highlighted {
    [super setHighlighted:highlighted];
    
    if (highlighted) {
        self.backgroundColor = UIColorFromRGBA(kCommonBlueColor,1.0);
    }
    else {
        self.backgroundColor = [UIColor whiteColor];
    }
}
@end
